package com.tisson.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.tisson.database.FormatUtil;
import com.tisson.database.StatementUtil;
import com.tisson.pojo.AcDepartment;
import com.tisson.pojo.AdminArea;
import com.tisson.pojo.AdminDepart;

public class AdminAreaDAO {
	private String query;
	private ResultSet rs;
	private Statement stmt;

	public AdminArea findById(String id) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "select * from admin_area where id = '" + id + "'";
		rs = stmt.executeQuery(query);
		AdminArea adminArea = new AdminArea();
		while (rs.next()) {
			adminArea.setId(FormatUtil.ConvertUuid(rs.getBytes("id")));
			adminArea.setAdminId(FormatUtil
					.ConvertUuid(rs.getBytes("admin_id")));
			adminArea.setAreaId(FormatUtil.ConvertUuid(rs.getBytes("area_id")));
		}
		rs.close();
		stmt.close();
		return adminArea;
	}

	public List findByAdminId(String id) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "select * from admin_area where admin_id = '" + id + "'";
		rs = stmt.executeQuery(query);
		List list = new ArrayList();
		while (rs.next()) {
			AdminArea adminArea = new AdminArea();
			adminArea.setId(FormatUtil.ConvertUuid(rs.getBytes("id")));
			adminArea.setAdminId(FormatUtil
					.ConvertUuid(rs.getBytes("admin_id")));
			adminArea.setAreaId(FormatUtil.ConvertUuid(rs.getBytes("area_id")));
			list.add(adminArea);
		}
		rs.close();
		stmt.close();
		return list;
	}

	public List findAll() throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "select * from admin_area";
		rs = stmt.executeQuery(query);
		List list = new ArrayList();
		while (rs.next()) {
			AdminArea adminArea = new AdminArea();
			adminArea.setId(FormatUtil.ConvertUuid(rs.getBytes("id")));
			adminArea.setAdminId(FormatUtil
					.ConvertUuid(rs.getBytes("admin_id")));
			adminArea.setAreaId(FormatUtil.ConvertUuid(rs.getBytes("area_id")));
			list.add(adminArea);
		}
		rs.close();
		stmt.close();
		return list;
	}

	public void save(AdminArea adminArea) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "insert into admin_area(admin_id,area_id) values('"
				+ adminArea.getAdminId() + "','" + adminArea.getAreaId() + "')";
		stmt.executeUpdate(query);
		stmt.close();
	}

	public void update(AdminArea adminArea) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "update admin_area set " + "admin_id = '"
				+ adminArea.getAdminId() + "',area_id = '"
				+ adminArea.getAreaId() + "'" + " where id = '"
				+ adminArea.getId() + "'";
		stmt.executeUpdate(query);
		stmt.close();
	}

	public void delete(String id) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "delete from admin_area where id = '" + id + "'";
		stmt.executeUpdate(query);
		stmt.close();
	}

	public void delete(AdminArea adminArea) throws SQLException {
		stmt = StatementUtil.getStatement();
		query = "delete from admin_area where id = '" + adminArea.getId() + "'";
		stmt.executeUpdate(query);
		stmt.close();
	}
}
